package com.google.code.b0rx0r.program;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractTriggerable implements Serializable, Triggerable {
	private String name;
	private Color color;
	
	private int note;
	
	private transient List<TriggerableListener> listeners = new ArrayList<TriggerableListener>();
	
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		in.defaultReadObject();
		
		listeners = new ArrayList<TriggerableListener>();
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		if (this.name != name) {
		  this.name = name;
		  fireChanged();
		}
	}
	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		if (this.color != color) {
		  this.color = color;
		  fireChanged();
		}

	}
	private void fireChanged() {
		for (TriggerableListener tl : listeners) {
			tl.triggerableChanged(this);
		}
	}
	
	public void addTriggerableListener(TriggerableListener tl) {
		listeners.add(tl);
	}

	public void removeTriggerableListener(TriggerableListener tl) {
		listeners.remove(tl);
	}
	
	public int getNote() {
		return note;
	}
	public void setNote(int note) {
		if (this.note != note) {
		  this.note = note;
		  fireChanged();
		}
	}

	@Override
	public String toString() {
		return getName();
	}
	
	
	

}
